package com.qfedu.wfx.manager.Controller;

import com.qfedu.wfx.manager.user.configuration.LoginUserBean;
import com.qfedu.wfx.manager.user.po.SysModule;
import com.qfedu.wfx.manager.user.service.IUserService;
import com.qfedu.wfx.manager.vo.JsonResultVO;
import com.qfedu.wfx.manager.vo.ParentNodeVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResultVO login(String username,String password){
        JsonResultVO jsonResultVO = new JsonResultVO();
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            SecurityUtils.getSubject().login(token);
            jsonResultVO.setCode(1);
        }catch (AuthenticationException e){
            e.printStackTrace();
            jsonResultVO.setCode(0);
        }

        return jsonResultVO;
    }
    @RequestMapping("/allfunc")
    public String queryPermission(Model model) throws Exception{
        LoginUserBean user = (LoginUserBean)SecurityUtils.getSubject().getSession().getAttribute("user");
        List<SysModule> noHasModule = userService.queryAllNoHasModule(user.getRoleCode());
        List<SysModule> moduleList = userService.queryModuleByRole(user.getRoleCode());
        model.addAttribute("noHasModule",noHasModule);
        model.addAttribute("moduleList",moduleList);
        return "permission";
    }
    @RequestMapping("/tree")
    @ResponseBody
    public List<ParentNodeVO> queryParentNode() throws Exception{
        LoginUserBean user = (LoginUserBean)SecurityUtils.getSubject().getSession().getAttribute("user");
        List<ParentNodeVO> parentNodeVOS = userService.queryParentNode(user.getRoleCode());
        return parentNodeVOS;
    }
    @RequestMapping("/addfunc")
    @ResponseBody
    public JsonResultVO addFunc(String moduleId,String roleId){
        JsonResultVO jsonResultVO = new JsonResultVO();
        try {
            userService.addFunc(moduleId, roleId);
            jsonResultVO.setCode(1);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVO.setCode(0);
        }
        return jsonResultVO;
    }

    @RequestMapping("/delfunc")
    @ResponseBody
    public JsonResultVO deleteFuncByCode(String moduleCode){
        String[] strings = new String[1];
        strings[0] = moduleCode;
        JsonResultVO jsonResultVO = new JsonResultVO();
        try {
            userService.deleteFuncByCode(strings);
            jsonResultVO.setCode(1);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultVO.setCode(0);
        }
        return jsonResultVO;
    }
}
